package org.kjtc.balanceController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.kjtc.balanceEntity.LineBalance;
import org.kjtc.balanceEntity.StageSummary;
import org.kjtc.balanceEntity.SubBranchCompanyBalance;
import org.kjtc.balanceEntity.SummaryBalance;
import org.kjtc.balanceService.BalanceService;
import org.kjtc.controller.BaseController;
import org.kjtc.util.DoubleUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BalanceController extends BaseController{

	@Resource(name = "balanceService")
	private BalanceService balanceService;

	@RequestMapping(value = "/searchSummaryBalance")
	@ResponseBody
	public Map<String, Object> searchSummaryBalance(SummaryBalance summaryBalance){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "汇总结算查询开始");

		List<SummaryBalance> list = null;
		int totalChargingCount = 0;
		double allTotalTradeKWH = 0;
		double allTotalChargingKWH = 0;
		double totalChargingPrice = 0;
		try {
			list = balanceService.getSummaryBalance(summaryBalance);
			for(int i=0; i<list.size(); i++){
				totalChargingCount += list.get(i).getChargingCount();
				allTotalTradeKWH = DoubleUtil.add(allTotalTradeKWH, list.get(i).getTotalTradeKWH());
				allTotalChargingKWH = DoubleUtil.add(allTotalChargingKWH, list.get(i).getTotalChargingKWH());
				totalChargingPrice = DoubleUtil.add(totalChargingPrice, list.get(i).getTotalPrice());
			}
			if(list.size() > 0) {
				this.getSession().setAttribute("summaryBalanceListTitle", summaryBalance.getBalanceMonth());
				this.getSession().setAttribute("summaryBalanceList", list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "汇总结算查询异常" + e.getMessage());
			return map;
		}
		map.put("totalChargingCount", totalChargingCount);
		map.put("allTotalTradeKWH", allTotalTradeKWH);
		map.put("allTotalChargingKWH", allTotalChargingKWH);
		map.put("totalChargingPrice", totalChargingPrice);
		map.put("summaryBalanceList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "汇总结算查询结束");
		summaryBalance = null;
		return map;
	}

	@RequestMapping(value = "/searchSubBranchCompanyBalance")
	@ResponseBody
	public Map<String, Object> searchSubBranchCompanyBalance(SubBranchCompanyBalance subBranchCompanyBalance){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司结算查询开始");

		List<SubBranchCompanyBalance> list = null;
//		int totalChargingCount = 0;
		double allTotalChargingKWH = 0;
		double totalChargingPrice = 0;
		double totalServicePrice = 0;
		double totalPrice = 0;
		try {
			list = balanceService.getBranchCompanyBalance(subBranchCompanyBalance);
			for(int i=0; i<list.size(); i++){
//				totalChargingCount += list.get(i).getChargingCount();
				allTotalChargingKWH = DoubleUtil.add(allTotalChargingKWH, list.get(i).getTotalChargingKWH());
				totalChargingPrice = DoubleUtil.add(totalChargingPrice, list.get(i).getChargingPrice());
				totalServicePrice = DoubleUtil.add(totalServicePrice, list.get(i).getServicePrice());
				totalPrice = DoubleUtil.add(totalPrice, list.get(i).getTotalPrice());
			}
			String balanceMonth = subBranchCompanyBalance.getBalanceMonth();
			if(list.size() > 0) {
				this.getSession().setAttribute("subBranchCompanyBalanceListTitle", list.get(0).getSubBranchCompanyName() + balanceMonth.substring(0, 4) + "年" + String.valueOf(Integer.parseInt(balanceMonth.substring(4, 6))).concat("月结算汇总表"));
				this.getSession().setAttribute("subBranchCompanyBalanceList", list);
				this.getSession().setAttribute("allTotalChargingKWH", allTotalChargingKWH);
				this.getSession().setAttribute("totalChargingPrice", totalChargingPrice);
				this.getSession().setAttribute("totalServicePrice", totalServicePrice);
				this.getSession().setAttribute("totalPrice", totalPrice);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司结算查询异常" + e.getMessage());
			return map;
		}
//		map.put("totalChargingCount", totalChargingCount);
		map.put("allTotalChargingKWH", allTotalChargingKWH);
		map.put("totalChargingPrice", totalChargingPrice);
		map.put("subBranchCompanyBalanceList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "分公司结算查询结束");
		subBranchCompanyBalance = null;
		return map;
	}

	@RequestMapping(value = "/searchLineBalance")
	@ResponseBody
	public Map<String, Object> searchLineBalance(LineBalance lineBalance){

		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "线路结算查询开始");

		List<LineBalance> list = null;
		int totalChargingCount = 0;
		double allTotalChargingKWH = 0;
		double totalChargingPrice = 0;
		try {
			list = balanceService.getLineBalance(lineBalance);
			for(int i=0; i<list.size(); i++){
				totalChargingCount += list.get(i).getChargingCount();
				allTotalChargingKWH = DoubleUtil.add(allTotalChargingKWH, list.get(i).getTotalChargingKWH());
				totalChargingPrice = DoubleUtil.add(totalChargingPrice, list.get(i).getTotalPrice());
			}
			totalChargingPrice = DoubleUtil.round(totalChargingPrice, 2);
			if(list.size() > 0) {
				this.getSession().setAttribute("lineBalanceListTitle", list.get(0).getSubBranchCompanyName() + list.get(0).getLineName() + "结算明细表");
				this.getSession().setAttribute("lineBalanceList", list);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "线路结算查询异常" + e.getMessage());
			return map;
		}
		map.put("totalChargingCount", totalChargingCount);
		map.put("allTotalChargingKWH", allTotalChargingKWH);
		map.put("totalChargingPrice", totalChargingPrice);
		map.put("LineBalanceList", list);
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "线路结算查询结束");
		lineBalance = null;
		return map;
	}
	
	@RequestMapping(value = "/searchStageSummary")
	@ResponseBody
	public Map<String, Object> searchStageSummary (StageSummary stageSummary) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "建设期统计查询");
		
		List<StageSummary> list = null;
		try {
			list = balanceService.getStageSummary(stageSummary);
			for (StageSummary stageSummary2 : list) {
				stageSummary2.setChargingPrice(DoubleUtil.round(stageSummary2.getChargingPrice(), 2));
				stageSummary2.setServicePrice(DoubleUtil.round(stageSummary2.getServicePrice(), 2));
			}
			this.getSession().setAttribute("stageSummaryList", list);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(this.getUesrInfo().getUserName() + "(" + this.getUesrInfo().getUserID() + ")：" + "建设期统计查询异常" + e.getMessage());
			return map;
		}
		map.put("stageSummaryList", list);
		return map;
	}
}